package cn.com.ccxe.dao.impl;

import java.util.List;

import cn.com.ccxe.core.entity.Keyword;
import cn.com.ccxe.core.entity.MonitorType;
import cn.com.ccxe.core.entity.Website;
import cn.com.ccxe.dao.DBHandler;
import cn.com.ccxe.dao.MonitorTypeDao;
import cn.com.ccxe.dao.OperationStatus;
import cn.com.ccxe.dao.parser.MonitorTypeParser;


public class MonitorTypeDaoImpl extends DBHandler implements MonitorTypeDao {

	
	/** {@inheritDoc} */
	public OperationStatus add(MonitorType monitorType) {
		String sql = "INSERT INTO C_MONITOR_TYPE(ID,TYPE,ISSTART) " +
				" VALUES(SEQ_C_MONITOR_TYPE.NEXTVAL,?,?)";
		
		Object [] objs = new Object[]{
				monitorType.getType(),
				monitorType.getIsStart()};
		return insertOrUpdateOrDelete(sql, objs);
	}
	
	/** {@inheritDoc} */
	public OperationStatus del(int monitorTypeId) {
		String sql = "DELETE C_MONITOR_TYPE WHERE ID=?";

		return insertOrUpdateOrDelete(sql, new Object[] { monitorTypeId });
	}

	/** {@inheritDoc} */
	public OperationStatus update(int id, MonitorType monitorType) {
		String sql = "UPDATE C_MONITOR_TYPE " 
			+ "SET TYPE=?,ISSTART=? "
			+ "WHERE ID=?";
		
		Object[] objs = new Object[] { 
				monitorType.getType(),
				monitorType.getIsStart(),
				id };
		return insertOrUpdateOrDelete(sql, objs);
	}

	/** {@inheritDoc} */
	public OperationStatus update(MonitorType monitorType) {
		return update(monitorType.getId(), monitorType);
	}

	/** {@inheritDoc} */
	public List<MonitorType> selectAll() throws Exception {
		String sql = 
				"SELECT A.ID monitorId,A.TYPE monitorType,A.ISSTART isStart " +
				"FROM C_MONITOR_TYPE A";
		return queryList(sql, null, new MonitorTypeParser());
	}
	
	/** {@inheritDoc} */
	public List<MonitorType> getAllByKeywordId(int keywordId) {
		String sql = 
			"SELECT A.ID monitorId,A.TYPE monitorType,A.ISSTART isStart " 
			+"FROM C_MONITOR_TYPE A, C_KEYWORD_TYPE B "
			+"WHERE B.KEYWORD_ID=? AND A.ID=B.MONITOR_TYPE_ID";
		
		return queryList(sql, new Object[]{keywordId}, new MonitorTypeParser());
	}

	/** {@inheritDoc} */
	public List<MonitorType> getAllByWebsiteId(int websiteId) {
		String sql = 
			"SELECT A.ID monitorId,A.TYPE monitorType,A.ISSTART isStart " 
			+"FROM C_MONITOR_TYPE A, C_WEBSITE_TYPE B "
			+"WHERE B.WEBSITE_ID=? AND A.ID=B.MONITOR_TYPE_ID";
		
		return queryList(sql, new Object[]{websiteId}, new MonitorTypeParser());
	}

	

	/**{@inheritDoc}
	 * 现就只有{@link Keyword}和{@link Website}与之有关系。
	 * 
	 */
	public int countRelationship(int monitorTypeId) {
		String sql = 
				"SELECT COUNT(*) "+
				"FROM C_WEBSITE_TYPE A , C_KEYWORD_TYPE B "  +
				"WHERE A.MONITOR_TYPE_ID=? AND B.MONITOR_TYPE_ID=?";
		return queryInt(sql, new Object[]{monitorTypeId,monitorTypeId});
	}

	public MonitorType get(int id) {
		String sql = 
					"SELECT A.ID monitorId,A.TYPE monitorType,A.ISSTART isStart " +
					"FROM C_MONITOR_TYPE A " +
					"WHERE A.ID=?";
		return queryObject(sql, new Object[]{id}, new MonitorTypeParser());
	}
	
}
